<template>
  <!-- 评分 -->
  <div class="pho-form-item">
    <van-field
      v-bind="$attrs"
      :label="showLabel ? $attrs.label : ''"
      :name="$attrs.__vModel__"
      :required="$attrs.required"
      :rules="$attrs.regList"
      :placeholder="$attrs.placeholder"
      input-align="right"
    >
      <template #input>
        <van-rate
          v-model="formValue"
          v-bind="$attrs"
          :count="$attrs.max"
          touchable
          @change="onChange"
        />
        <!-- 显示分数 -->
        <span v-if="$attrs.showScore" style="margin-left: 5px">{{
          formValue
        }}</span>
        <!-- 显示文字 -->
        <span v-if="$attrs.showText" style="margin-left: 5px">{{
          texts[Math.ceil(formValue) - 1]
        }}</span>
      </template>
    </van-field>
  </div>
</template>

<script>
export default {
  name: "phone-form-rate",
  model: {
    prop: "value", // 绑定的值，通过父组件传递
    event: "update",
  },
  props: {
    value: {
      type: [Number, String],
      default: "",
    },
    // 是否展示标题
    showLabel: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      formValue: 0,
      texts: ["极差", "失望", "一般", "满意", "惊喜"], // 辅助文字
    };
  },
  watch: {
    value: {
      immediate: true,
      deep: true,
      handler(newValue) {
        this.formValue = newValue;
      },
    },
  },
  methods: {
    onChange(val) {
      this.$emit("update", val);
    },
  },
};
</script>

<style lang="scss" scoped></style>
